package com.cuit.zuke.consumer.controller;

import com.cuit.zuke.consumer.pojo.Comment;
import com.cuit.zuke.consumer.service.impl.CommentServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/consumer/comment")
public class CommentController {
    @Autowired
    private CommentServiceImpl commentService;

    @RequestMapping("/queryAll")
    public Map<String,Object> queryAll(int currentPage, int pageSize) {
        currentPage = (currentPage-1)*pageSize;
        int total = commentService.queryTotal();
        List<Comment> comments = commentService.queryAll(currentPage, pageSize);
        System.out.println(comments);
        Map<String,Object> map = new HashMap<>();
        map.put("list", comments);
        map.put("total", total);
        return map;
    }
    @RequestMapping("/queryByUserId")
    public Map<String,Object> queryCommentByUserId(String userId, int currentPage, int pageSize) {
        System.out.println(userId+" "+currentPage+" "+pageSize);
        currentPage = (currentPage-1)*pageSize;
        List<Comment> comments = commentService.queryCommentByUserId(userId, currentPage, pageSize);
        int total = commentService.queryOneTotal(userId);
        System.out.println(comments);
        Map<String,Object> map = new HashMap<>();
        map.put("list", comments);
        map.put("total", total);
        //        System.out.println(comments);
        return map;
    }

    /**
     *修改评论逻辑删除
     * @param commentId
     * @return
     */
    @RequestMapping("/update")
    public int updateIsDeleted(String commentId) {
        Comment comment = commentService.queryCommentByCId(commentId);
        if(comment.getIsDeleted() == 1) {
            commentService.updateIsDeleted(commentId, 0);
        }else {
            commentService.updateIsDeleted(commentId, 1);
        }
//        System.out.println(comment.getIsDeleted());
        return 0;
    }

    @RequestMapping("/multipleUpdate")
    public int multipleUpdate(@RequestBody Long[] commentList) {
        Comment comment;
        for (Long aLong : commentList) {
            String commentId = String.valueOf(aLong);
            comment = commentService.queryCommentByCId(commentId);
            if(comment!=null){
                if(comment.getIsDeleted() == 1) {
                    commentService.updateIsDeleted(commentId, 0);
                }else {
                    commentService.updateIsDeleted(commentId, 1);
                }
            }
        }
        return 0;
    }

    @GetMapping("/queryByHotelId/{id}")
    public List<Comment> queryByHotelId(@PathVariable String id)  {
        System.out.println("进入方法");
        List<Comment> names = commentService.queryUserByHotelId(id);
        System.out.println(names);
        return names;
    }

    @PostMapping("/insertHotelComment")
    public int insertHotelComment(@RequestParam("id") String hotelId, @RequestParam("content") String content
            , @RequestParam("userId") String userId) {
        commentService.insertHotelComment(hotelId, content, userId);
        return 0;
    }

    @PostMapping("/insertGoodComment")
    public int insertGoodComment(@RequestParam("id") String hotelId, @RequestParam("content") String content
            , @RequestParam("userId") String userId) {
        commentService.insertGoodComment(hotelId, content, userId);
        return 0;
    }
}
